<template>
	<view class="container">
		<view class="" v-if="!isLogin">
			<uni-forms ref="valiForm" :modelValue="info" :rules="rules">
				<uni-forms-item label="日期" required name="riqi" label-position="top">
					<uni-datetime-picker type="datetime" v-model="info.riqi" @change="dateChangeLog" />
				</uni-forms-item>

				<uni-forms-item label="评分" required name="pingfen" label-position="top">
					<uni-rate :max="10" v-model="info.pingfen" color="#bbb" active-color="#fd328a"
						@change="rateChange" />
				</uni-forms-item>

				<uni-forms-item label="类型" required name="leixing" label-position="top">
					<uni-data-checkbox mode="tag" v-model="info.leixing" :localdata="jiangliarr"></uni-data-checkbox>
				</uni-forms-item>

				<uni-forms-item label="金额(单位:元)" required name="money" label-position="top" label-width="200">
					<!-- <uni-number-box v-model="info.money" /> -->
					<!-- <input type="digit" v-model="info.money" class="jine" /> -->
					<uni-easyinput type="input" v-model="info.money"></uni-easyinput>
				</uni-forms-item>

				<uni-forms-item label="奖惩原因" required name="yuanyin" label-position="top">
					<uni-easyinput type="textarea" v-model="info.yuanyin" placeholder="请输入奖惩原因"></uni-easyinput>
				</uni-forms-item>

			</uni-forms>
			<button type="primary" @tap="add('valiForm')">提 交</button>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				href: 'https://uniapp.dcloud.io/component/README?id=uniui',
				isLogin: false,
				info: {
					riqi: '',
					pingfen: '',
					leixing: '',
					yuanyin: '',
					money: 0,
				},

				jiangliarr: [{
					text: '奖励',
					value: 1
				}, {
					text: '惩罚',
					value: 2
				}],

				rules: {
					riqi: {
						rules: [{
							required: true,
							errorMessage: '日期不能为空'
						}]
					},
					pingfen: {
						rules: [{
							required: true,
							errorMessage: '评分不能为空'
						}]
					},
					leixing: {
						rules: [{
							required: true,
							errorMessage: '请选择奖惩类型'
						}]
					},
					money: {
						rules: [{
							required: true,
							errorMessage: '金额不能为空'
						}]
					},
					yuanyin: {
						rules: [{
							required: true,
							errorMessage: '奖惩原因不能为空'
						}]
					}
				},
			}
		},
		methods: {
			//日期选择
			dateChangeLog(e) {
				console.log(e)
			},
			//评星
			rateChange(e) {
				console.log(e.value)
			},
			//新增记录
			add(ref) {
				uni.showLoading({
					title: 'loading'
				})
				this.$refs[ref].validate().then(res => {
					// console.log('success', res);
					let userinfo = uni.getStorageSync('LOGIN')
					let info = Object.assign(this.info, {
						userid: userinfo._id,
						money: Number(this.info.money)
					})
					console.log(info)
					uniCloud.callFunction({
						name: 'add-jiangcheng',
						data: info
					}).then(res => {
						console.log(res)
						uni.hideLoading()
						if (res.result.updated) {

							uni.showModal({
								content: '添加成功',
								showCancel: false,
								success: (res) => {
									if (res.confirm) {
										this.info = {
											riqi: '',
											rateValue: 0,
											radio3: '',
											yuanyin: '',
											money: 2,
										}
									}
								}
							})
						}


					})

				}).catch(err => {
					uni.hideLoading()
					console.log('err', err);
				})
			}
		},
		onShow() {
			console.log('show')
			this.isLogin = !uni.getStorageSync('LOGIN')
			if (!uni.getStorageSync('LOGIN')) {
				uni.showModal({
					content: '请先登录',
					showCancel: false,
					success: (res) => {
						if (res.confirm) {
							uni.switchTab({
								url: '/pages/my/my'
							})
						}
					}
				})


			}
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}

	.jine {
		border: 1px solid #e9e9e9;
		padding: 0 20rpx;
	}
</style>
